//
//  WBHomeTableViewCell.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/21.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit
import SnapKit

class WBHomeTableViewCell: UITableViewCell {

    // MARK: -  记录我们的底部toolBar 的约束
    var toolBarTopConstraint: Constraint?
    
    // MARK: -  模型属性
    var status: WBStatusModel? {
        
        didSet {
            
            originalView.status = status
            
            //卸载约束
            self.toolBarTopConstraint?.uninstall()
            
            if status?.retweeted_status == nil {
                
                retWeetView.hidden = true
                statusToolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }else {
                
                retWeetView.hidden = false
                retWeetView.status = status
                statusToolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(retWeetView.snp_bottom).constraint
                })
            }
            
            statusToolBarView.status = status
        }
    }
    
    // MARK: -  重写init方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }

    // MARK: -  重写init方法时必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: -  设置cell的视图
    private func setUpUI() {
        
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retWeetView)
        contentView.addSubview(statusToolBarView)
        
        
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(contentView)
        }
        
        retWeetView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        statusToolBarView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(contentView)
            self.toolBarTopConstraint = make.top.equalTo(retWeetView.snp_bottom).constraint
            make.height.equalTo(37)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(statusToolBarView)
        }
    }
    

    
    
    // MARK: -  懒加载控件
    
    // MARK: -  原创微博
    private lazy var originalView: WBOriginalView = WBOriginalView()
    
    // MARK: -  转发微博
    private lazy var retWeetView: WBStatusRetWeetView = WBStatusRetWeetView()
    
    // MARK: -  底部toolBar
    private lazy var statusToolBarView: WBStatusToolBarView = {
        
        let statusToolBarView = WBStatusToolBarView()
        return statusToolBarView
    }()
}
